package com.geeksaga.web;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.geeksaga.common.util.DateUtil;
import com.geeksaga.common.util.PaginationUtil;
import com.geeksaga.domain.Issue;
import com.geeksaga.domain.Project;
import com.geeksaga.service.IssueService;
import com.geeksaga.service.ProjectService;

/**
 * @author newbie
 * @since 0.1
 */
@Controller
public class IndexController {

    protected static final Log logger = LogFactory.getLog(IndexController.class);
    
    @Autowired private IssueService issueService = null;
    @Autowired private ProjectService projectService = null;
    
    @RequestMapping(value = { "/issue/index.do" }, method = RequestMethod.GET)
    public String issueIndex(@RequestParam(required = false, defaultValue = "0") int size, ModelMap model) {

        PaginationUtil pagination = new PaginationUtil();
        pagination.setSize((size > 0) ? size : 5);
        
        Map<String, Object> parameter = pagination.getParameter();
        
        List<Issue> newList = issueService.recentList(parameter);
        
        parameter.put("state", "B00601");
        parameter.put(PaginationUtil.PAGE_SIZE, 10);
        
        List<Issue> processList = issueService.recentList(parameter);
        
        List<Project> projectList = projectService.list();
        
        Issue issue = new Issue();
        issue.setIssueContent("What's on your issue?");
        
        model.addAttribute("newList", (newList != null) ? newList : new ArrayList<Issue>());
        model.addAttribute("processList", (processList != null) ? processList : new ArrayList<Issue>());
        model.addAttribute("projectList", (projectList != null) ? projectList : new ArrayList<Project>());
        model.addAttribute("issue", issue != null ? issue : new Issue());
        model.addAttribute("basicDate", DateUtil.getBasicDate());
        
        return "issueIndex";
    }
    
    @RequestMapping(value = { "/sns/home.do" }, method = RequestMethod.GET)
    public String snsHome(@RequestParam(required = false, defaultValue = "0") int size, ModelMap model,
            HttpServletRequest request) {
        
        PaginationUtil pagination = new PaginationUtil();
        pagination.setSize((size > 0) ? size : 5);
        
        Map<String, Object> parameter = pagination.getParameter();
        
        List<Issue> newList = issueService.recentList(parameter);
        
        parameter.put("state", "B00601");
        parameter.put(PaginationUtil.PAGE_SIZE, 10);
        
        List<Issue> processList = issueService.recentList(parameter);
        
        Issue issue = new Issue();
        issue.setIssueContent("What's on your issue?");
        
        model.addAttribute("newList", (newList != null) ? newList : new ArrayList<Issue>());
        model.addAttribute("processList", (processList != null) ? processList : new ArrayList<Issue>());
        model.addAttribute("issue", issue != null ? issue : new Issue());
        model.addAttribute("basicDate", DateUtil.getBasicDate());
        
        return "snsHome";
    }
    
    @RequestMapping(value = { "/sns/index.do" }, method = RequestMethod.GET)
    public String snsIndex(ModelMap model) {
        
        model.addAttribute("basicDate", DateUtil.getBasicDate());
        
        return "snsIndex";
    }
}